package com.jfs.qy.chain.component;

import com.jfs.qy.chain.AbstractServerStartHandler;
import com.jfs.qy.config.component.SystemIpcListInfoTask;
import com.jfs.qy.config.component.TaskExecutorReactor;
import com.jfs.qy.entity.response.SearchIpcListInfoResponse;
import com.jfs.qy.enums.NvrApiEnum;
import com.jfs.qy.service.SystemIpcListInfoService;
import com.jfs.qy.session.NvrSessionStorage;
import com.jfs.qy.utils.SystemCommonUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.CountDownLatch;

/**
 * @Description 查询摄像头列表详情链
 * @E-mail cwm3412638@163.com
 * @Author cwm
 * @Date 2022/11/5
 */
@Component(value = SystemIpcListInfoComponent.BEAN_NAME)
public class SystemIpcListInfoComponent extends AbstractServerStartHandler implements SystemIpcListInfoService {
    public final static String BEAN_NAME = "systemIpcListInfoComponent";
    private final SystemCommonUtils systemCommonUtils;

    private final RestTemplate restTemplate;
    private final TaskExecutorReactor taskExecutorReactor;
    /**
     * 下一个责任链对象
     *
     * @param abstractServerStartHandler
     */
    private final AbstractServerStartHandler abstractServerStartHandler;

    public SystemIpcListInfoComponent(@Qualifier(value = AlarmEventComponent.BEAN_NAME)
                                      AbstractServerStartHandler abstractServerStartHandler,
                                      TaskExecutorReactor taskExecutorReactor,
                                      RestTemplate restTemplate,
                                      SystemCommonUtils systemCommonUtils) {
        this.abstractServerStartHandler = abstractServerStartHandler;
        this.taskExecutorReactor = taskExecutorReactor;
        this.restTemplate = restTemplate;
        this.systemCommonUtils = systemCommonUtils;
    }


    @Override
    public void handlerOperation() throws URISyntaxException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
        SystemIpcListInfoTask systemIpcListInfoTask = new SystemIpcListInfoTask(this);
        taskExecutorReactor.schedule(systemIpcListInfoTask,0, systemCommonUtils.getSystemIpcListInfoInterval());
        //调用下一个责任链
        abstractServerStartHandler.handlerOperation();
    }

    @Override
    public ResponseEntity<SearchIpcListInfoResponse> searchIpcListInfo() {
        String requestUrl = systemCommonUtils.getRequestUrl(NvrApiEnum.CHANNEL_INFO);
        final HttpEntity<?> entity = new HttpEntity<>(null, NvrSessionStorage.headers);
        return restTemplate.exchange(requestUrl, HttpMethod.POST, entity, SearchIpcListInfoResponse.class);
    }
}
